package me.baicai.weixin.identity.service.impl;

import me.baicai.utils.StringUtils;
import me.baicai.weixin.application.BasicService;
import me.baicai.weixin.application.Result;
import me.baicai.weixin.entity.gcb.Advertising;
import me.baicai.weixin.entity.gcb.PageEntity;
import me.baicai.weixin.entity.gcb.Query;
import me.baicai.weixin.identity.repository.AdvertisingRepository;
import me.baicai.weixin.identity.service.AdverTisingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class AdverTisingServiceImpl extends BasicService implements AdverTisingService
{

    @Autowired
    private AdvertisingRepository advertisingRepository;

    private static List<Advertising> advertisings1 = new ArrayList<>();

    private static List<Advertising> advertisings2 = new ArrayList<>();

    private static List<Advertising> advertisings3 = new ArrayList<>();

    private static List<Advertising> advertisings4 = new ArrayList<>();

    private static List<Advertising> advertisings5 = new ArrayList<>();

    static
    {
        Advertising advertising1 = new Advertising(1, "http://file.gcb360.com/1.jpg");
        Advertising advertising2 = new Advertising(2, "http://file.gcb360.com/2.jpg");
        Advertising advertising3 = new Advertising(3, "http://file.gcb360.com/3.jpg");
        Advertising advertising4 = new Advertising(4, "http://file.gcb360.com/4.jpg");
        Advertising advertising5 = new Advertising(5, "http://file.gcb360.com/5.jpg");

        advertisings1.add(advertising1);
        advertisings2.add(advertising1);
        advertisings2.add(advertising2);
        advertisings3.add(advertising1);
        advertisings3.add(advertising2);
        advertisings3.add(advertising3);
        advertisings4.add(advertising1);
        advertisings4.add(advertising2);
        advertisings4.add(advertising3);
        advertisings4.add(advertising4);
        advertisings5.add(advertising1);
        advertisings5.add(advertising2);
        advertisings5.add(advertising3);
        advertisings5.add(advertising4);
        advertisings5.add(advertising5);
    }

    @Override
    public Result advertising(Query query)
    {
        Pageable pageable = new PageEntity(query.getPage(), query.getSize(), Sort.Direction.DESC,
            "id");
        if (StringUtils.isNotBlank(query.getType()) && StringUtils.isNotBlank(query.getValue()))
        {
            switch (query.getType())
            {
                case "id":
                    return this.success(
                        advertisingRepository.findAllById(Integer.valueOf(query.getValue()),
                            pageable));
            }
        }
        return this.success(advertisingRepository.findAll(pageable));
    }

    @Override
    public Result findAll()
    {
        return this.success(advertisingRepository.findAll());
    }

    @Override
    public Result advertisingSet(Integer id, Integer status)
    {
        Advertising advertising = advertisingRepository.getOne(id);
        advertising.setStatus(status);
        advertisingRepository.saveAndFlush(advertising);
        return this.success();
    }

    @Override
    public Result advertising(Advertising advertising)
    {
        advertisingRepository.saveAndFlush(advertising);
        return this.success();
    }

    /**
     * 小程序随机区域广告
     *
     * @param region
     * @return
     */

    @Override
    public Result gcbAdvertising(String city, String region)
    {
        List<Advertising> list = advertisingRepository.findAllByRegionLike("%" + city + "%","%" + region + "%",4);
        if (null == list || list.size() == 0)
        { // 公司自带的最后一张图
            return this.success(advertisings5);
        }
        switch (list.size()){
            case 1 :
                list.addAll(advertisings4);
                break;
            case 2 :
                list.addAll(advertisings3);
                break;
            case 3 :
                list.addAll(advertisings2);
                break;
            case 4 :
                list.addAll(advertisings1);
                break;
        }

        return this.success(list);
    }

    @Override
    public List<Advertising> shopAdvertising(Integer shopid, String city, String region)
    {
        List<Advertising> list = advertisingRepository.findAllByShopId(shopid);
        if (null == list || list.size() == 0)
        { // 公司自带的最后一张图
            return null;
        }
        list.addAll(advertisingRepository.findAllByRegionLike("%" + city + "%", "%" + region + "%",5 - list.size()));
        switch (list.size()){
            case 1 :
                list.addAll(advertisings4);
                break;
            case 2 :
                list.addAll(advertisings3);
                break;
            case 3 :
                list.addAll(advertisings2);
                break;
            case 4 :
                list.addAll(advertisings1);
                break;
        }
        return list;
    }
}
